import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

NProgress.configure({ showSpinner: false });
const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

Vue.use(VueRouter);

const routes = [
    {
        path: "/",
        redirect: "login",
    },
    {
        path: "/login1",
        name: "login1",
        component: () => import("@/views/login/login1.vue"),
        meta: {
            title: "登录",
            requireAuth: false,
        },
    },
    {
        path: "/login2",
        name: "login2",
        component: () => import("@/views/login/login2.vue"),
        meta: {
            title: "登录",
            requireAuth: false,
        },
    },
    {
        path: "/login3",
        name: "login3",
        component: () => import("@/views/login/login3.vue"),
        meta: {
            title: "登录",
            requireAuth: false,
        },
    },
    {
        path: "/login",
        name: "login",
        component: () => import("@/views/login/login.vue"),
        meta: {
            title: "登录",
            requireAuth: false,
        },
    },
    {
        path: "/mail",
        name: "mail",
        component: () => import("@/views/mail.vue"),
        meta: {
            title: "通讯录",
            requireAuth: true,
        },
    },
    {
        path: "/help",
        name: "help",
        component: () => import("@/views/help.vue"),
        meta: {
            title: "帮助与反馈",
            requireAuth: true,
        },
    },
    {
        path: "/info",
        name: "info",
        component: () => import("@/views/info.vue"),
        meta: {
            title: "信息中心",
            requireAuth: true,
        },
    },
    {
        path: "/activity",
        name: "activity",
        component: () => import("@/views/activity.vue"),
        meta: {
            title: "活动推广",
            requireAuth: true,
        },
    },
    {
        path: "/report",
        name: "report",
        component: () => import("@/views/report.vue"),
        meta: {
            title: "业绩报告",
            requireAuth: true,
        },
    },
    {
        path: "/mailDetail/:id",
        name: "mailDetail",
        component: () => import("@/views/mailDetail.vue"),
        meta: {
            title: "通讯录",
            requireAuth: true,
        },
    },
    {
        path: "/userInfo",
        name: "userInfo",
        component: () => import("@/views/userInfo.vue"),
        meta: {
            title: "通讯录",
            requireAuth: true,
        },
    },
    {
        path: "/helpDetail",
        name: "helpDetail",
        component: () => import("@/views/helpDetail.vue"),
        meta: {
            title: "问题详情",
            requireAuth: true,
        },
    },
    {
        path: "/helpPost",
        name: "helpPost",
        component: () => import("@/views/helpPost.vue"),
        meta: {
            title: "填写反馈",
            requireAuth: true,
        },
    },
    {
        path: "/infoList",
        name: "infoList",
        component: () => import("@/views/infoList.vue"),
        meta: {
            title: "信息中心",
            requireAuth: true,
        },
    },
    {
        path: "/seePdf",
        name: "seePdf",
        component: () => import("@/views/seePdf.vue"),
        meta: {
            title: "信息中心",
            requireAuth: true,
        },
    },
    {
        path: "/activityList",
        name: "activityList",
        component: () => import("@/views/activityList.vue"),
        meta: {
            title: "活动记录",
            requireAuth: true,
        },
    },
    {
        path: "/activityDetail",
        name: "activityDetail",
        component: () => import("@/views/activityDetail.vue"),
        meta: {
            title: "活动详情",
            requireAuth: true,
        },
    },
    {
        path: "/activityPost",
        name: "activityPost",
        component: () => import("@/views/activityPost.vue"),
        meta: {
            title: "活动申请",
            requireAuth: true,
        },
    },
    {
        path: "/reportList",
        name: "reportList",
        component: () => import("@/views/reportList.vue"),
        meta: {
            title: "业绩报告列表",
            requireAuth: true,
        },
    },
    {
        path: "/reportDesc",
        name: "reportDesc",
        component: () => import("@/views/reportDesc.vue"),
        meta: {
            title: "业绩报告列表",
            requireAuth: true,
        },
    },
    {
        path: "/searchLeads",
        name: "searchLeads",
        component: () => import("@/views/searchLeads.vue"),
        meta: {
            title: "Leads状态查询 ",
            requireAuth: true,
        },
    },
    {
        path: "/detailLead",
        name: "detailLead",
        component: () => import("@/views/detailLead.vue"),
        meta: {
            title: "Leads状态详情",
            requireAuth: true,
        },
    },
    {
        path: "/newActivity",
        name: "newActivity",
        component: () => import("@/views/newActivity.vue"),
        meta: {
            title: "活动推广",
            requireAuth: true,
        },
    },
    {
        path: "/newActivityList",
        name: "newActivityList",
        component: () => import("@/views/newActivityList.vue"),
        meta: {
            title: "活动记录",
            requireAuth: true,
        },
    },
    {
        path: "/newActivityPost",
        name: "newActivityPost",
        component: () => import("@/views/newActivityPost.vue"),
        meta: {
            title: "活动申请",
            requireAuth: true,
        },
    },
    {
        path: "/newInfo",
        name: "newInfo",
        component: () => import("@/views/newInfo.vue"),
        meta: {
            title: "信息中心",
            requireAuth: true,
        },
    },
    {
        path: "/newHelp",
        name: "newHelp",
        component: () => import("@/views/newHelp.vue"),
        meta: {
            title: "帮助与反馈",
            requireAuth: true,
        },
    },
    {
        path: "*",
        name: "error",
        component: () => import("@/views/error/index.vue"),
    },
];

const router = new VueRouter({
    // mode: "history", // 去掉url中的#
    scrollBehavior: () => ({ y: 0 }),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    let title = to.meta.title || to.name;
    document.title = title;
    let token = localStorage.getItem("_token_");
    // 判断路由是否需要登录权限
    if (to.meta.requireAuth && !token) {
        localStorage.setItem("_redirect", to.fullPath);
        NProgress.done();
        next({
            path: `/login`,
        });
    } else {
        next();
    }
});

router.afterEach(() => {
    NProgress.done();
});

export default router;
